package com.mushuilingfeng.ddd.user.domain.org.entity;

import cn.hutool.core.util.StrUtil;
import com.mushuilingfeng.ddd.user.domain.base.BaseEntity;
import com.mushuilingfeng.ddd.user.domain.base.BusinessException;
import com.mushuilingfeng.ddd.user.domain.base.Ii8nCode;
import com.mushuilingfeng.ddd.user.domain.org.enums.OrgApplyStatusEnum;
import com.mushuilingfeng.ddd.user.domain.org.event.OrgApplyCheckEvent;
import com.mushuilingfeng.ddd.user.domain.org.event.OrgApplySubmitEvent;
import com.mushuilingfeng.ddd.user.domain.org.repository.IOrgApplyRepository;
import com.mushuilingfeng.ddd.user.domain.org.service.IOrgApplyService;
import com.mushuilingfeng.ddd.user.domain.org.vo.OrgApplyRequest;
import com.mushuilingfeng.ddd.user.domain.org.vo.OrgApplyResult;
import com.mushuilingfeng.ddd.framework.factory.DomainEventFactory;
import com.mushuilingfeng.ddd.framework.factory.DomainServiceFactory;
import com.mushuilingfeng.ddd.framework.factory.DomainRepositoryFactory;
import lombok.Data;
import lombok.EqualsAndHashCode;

import java.util.Date;

/**
 * 机构申请单
 *
 * @author
 * @date 2024/9/18
 */
@EqualsAndHashCode(callSuper = true)
@Data
public class OrgApply extends BaseEntity {
    private String applyNo;
    private OrgApplyRequest orgApplyRequest;
    private OrgApplyResult orgApplyResult;
    private Org resultOrg;
    private Integer status;

    @Override
    public void simpleCheckParams() {
        // 检查参数
        orgApplyRequest.simpleCheckParams();
    }

    @Override
    public void saveNew() {
        super.saveNew();
        setApplyNo(DomainServiceFactory.getDomainService(IOrgApplyService.class).newOrgApplyNo());
        DomainRepositoryFactory.getRepository(IOrgApplyRepository.class).saveOrUpdate(this);
    }

    @Override
    public void update() {
        super.update();
        if (status != OrgApplyStatusEnum.DRAFT.getCode()) {
            throw new BusinessException(Ii8nCode.DATA_NOT_ALLOWED_TO_UPDATE);
        }
        // 检查参数是否合法
        if (StrUtil.isBlankIfStr(applyNo)) {
            throw new BusinessException(Ii8nCode.PARAM_INVALID);
        }
        if (null == orgApplyRequest) {
            throw new BusinessException(Ii8nCode.PARAM_INVALID);
        }
        DomainRepositoryFactory.getRepository(IOrgApplyRepository.class).saveOrUpdate(this);
    }

    public void submit() {
        if (status != OrgApplyStatusEnum.DRAFT.getCode()) {
            throw new BusinessException(Ii8nCode.DATA_NOT_ALLOWED_TO_UPDATE);
        }
        status = OrgApplyStatusEnum.WAIT_CHECK.getCode();
        setUpdateTime(new Date());
        DomainRepositoryFactory.getRepository(IOrgApplyRepository.class).saveOrUpdate(this);
        // 发布机构申请提交事件
        DomainEventFactory.getEventPublisher().publish(new OrgApplySubmitEvent(id));
    }

    public void doCheck(OrgApplyResult orgApplyResult) {
        if (status != OrgApplyStatusEnum.WAIT_CHECK.getCode()) {
            throw new BusinessException(Ii8nCode.DATA_NOT_NOT_IN_CHECK);
        }
        orgApplyResult.simpleCheckParams();
        this.orgApplyResult = orgApplyResult;
        status = OrgApplyStatusEnum.CHECK_PASS.getCode();
        DomainRepositoryFactory.getRepository(IOrgApplyRepository.class).saveOrUpdate(this);
        // 以布审核事件
        DomainEventFactory.getEventPublisher().publish(new OrgApplyCheckEvent(id));
    }

    public void associateResultOrg(Org org) {
        this.resultOrg = org;
        DomainRepositoryFactory.getRepository(IOrgApplyRepository.class).saveOrUpdate(this);
    }

}
